package SecondSaturday;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class chulManage extends JFrame {	
	
	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JTextField inId;
	private JButton btnIn;
	private JButton btnOut;
	private JScrollPane scrollPane;
	private JTextArea result;
	private JButton btnCheckLog;
	
	
	
	static String[] hist = new String[10];
	Chul[] chul = new Chul[10];
	int h=0; // 데이터가 저장될 위치
	boolean flag = false; // 1번이라도 10건 이상.
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					chulManage frame = new chulManage();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public chulManage() {
		setTitle("ChulCheck");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 247, 429);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getInId());
		contentPane.add(getBtnIn());
		contentPane.add(getBtnOut());
		contentPane.add(getScrollPane());
		contentPane.add(getBtnCheckLog());
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("ID");
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 15));
			lblNewLabel.setBounds(12, 10, 23, 15);
		}
		return lblNewLabel;
	}
	private JTextField getInId() {
		if (inId == null) {
			inId = new JTextField();
			inId.setBounds(47, 7, 171, 21);
			inId.setColumns(10);
		}
		return inId;
	}
	private JButton getBtnIn() {
		if (btnIn == null) {
			btnIn = new JButton("출근");
			btnIn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					check(true);				
					
				}
			});
			btnIn.setBounds(12, 35, 97, 23);
		}
		return btnIn;
	}
	private JButton getBtnOut() {
		if (btnOut == null) {
			btnOut = new JButton("퇴근");
			btnOut.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					check(false);
				}
			});
			btnOut.setBounds(121, 35, 97, 23);
		}
		return btnOut;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 68, 206, 280);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	private JButton getBtnCheckLog() {
		if (btnCheckLog == null) {
			btnCheckLog = new JButton("출퇴근 목록 확인");
			btnCheckLog.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
										
					ChulHistory frame1 = new ChulHistory(chul,h,flag); 					 
					
					frame1.setVisible(true); 
					
					
				}
			});
			btnCheckLog.setBounds(12, 358, 206, 23);
		}
		return btnCheckLog;
	}
	
	public void check(boolean a){ // 선생님 코드 
		// 출근에는 check(true);  퇴근에는 check(false);
		if(inId.getText().equals("")){
			JOptionPane.showMessageDialog(null, "아이디를 입력하세요.");
			return;
		}
				
		Chul c = null;
		String temp = "";
		if(a){
			c = new Chul(inId.getText(), new Date(), null, new Date());
			temp = "출근";
		}else{
			c = new Chul(inId.getText(), null, new Date(), new Date());
			temp = "퇴근";
		}
				
		chul[h] = c;
		if(h == 9){
			h=-1;
			flag=true;
		}
		h++;
		
		result.insert(c.toString() + temp + "\n",0); // 최근의 데이터를 맨위로.
		result.setCaretPosition(0); // 맨위를 보도록 명령.
		//result.append(c.toString() + temp + "\n");
	}
	public void chul_jt(){
		//result.append(String.valueOf(c.checkIn));
		
		// SimpleDateFormat
		SimpleDateFormat in = new SimpleDateFormat(" yy-MM-dd hh:mm ");					
		String inTime = in.format(new Date());
		String mor = inId.getText()+inTime+"입실\n";
		result.append(mor);
		if(h>9){						
			for(int i=0;i<hist.length-1;i++){							
				hist[i]=hist[i+1];							
			}
			hist[9]=mor;
			h++;
		}else{
			hist[h]=mor;
			h++;
		}				
	}
	public void toei_jt(){
		// SimpleDateFormat
		SimpleDateFormat out = new SimpleDateFormat(" yy-MM-dd hh:mm ");					
		String outTime = out.format(new Date());
		String ni = inId.getText()+outTime+"퇴실\n";
		result.append(ni);
		
		if(h>9){
			hist[0]="";
			for(int i=0;i<hist.length-1;i++){							
				hist[i]=hist[i+1];							
			}
			hist[9]=ni;
			h++;
		}else{
			hist[h]=ni;
			h++;
		}
	}
	
}
